class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size(), n = matrix[0].size(), i = 0;
        for(i = 0; i < m; i++)
        {
            if(matrix[i][n - 1] < target)
                continue;
            else
            {
                int left = 0, right = n - 1;
                while(left <= right)
                {
                    int mid = (left + right) / 2;
                    if(matrix[i][mid] > target)
                        right = mid - 1;
                    else if(matrix[i][mid] < target)
                        left = mid + 1;
                    else 
                        return true;
                }
                break;
            }
        }
        return false;
    }
};